#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 2020 Joyent, Inc.

# sdc-ssh: SSH to CN by hostname or uuid for Triton headnode

if [[ -n "$TRACE" ]]; then
    export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi

target="$1"
if (( ${#target} == 36 )); then
    # target is a uuid
    ip=$(sdc-server admin-ip "$target")
else
    # target is a hostname
    ip=$(sdc-cnapi /servers?hostname=${target}\&extras=sysinfo | json -Ha sysinfo.'Admin IP')
fi
if (( ${#ip} == 0 )); then
    printf 'Compute node "%s" not found\n' "$target"
    exit 1
fi

ssh -l root -i /root/.ssh/sdc.id_rsa "$ip"